package com.xiaoyu.lanling.feature.moment.data

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestDefaultHandler
import android.text.TextUtils
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.moment.DeleteMomentEvent
import com.xiaoyu.lanling.event.moment.MomentAccostEvent
import com.xiaoyu.lanling.event.moment.MomentReportEvent
import com.xiaoyu.lanling.event.moment.detail.DeleteCommentEvent

object MomentAction {

    fun report(requestTag: Any, fid: String, images: List<String>, text: String = "") {
        RequestCreator.create<MomentReportEvent>(requestTag, MomentReportEvent::class.java)
                .addPostData("objectId", fid)
                .addPostData("objectType", "feed")
                .addPostData("reason", "other")
                .addPostData("text", text)
                .addPostData("images", TextUtils.join(",", images))
                .setRequestUrl(HttpConfig.LANLING_REPORT)
                .enqueue()
    }

    fun accost(requestTag: Any, uid: String, fid: String) {
        RequestCreator.create<MomentAccostEvent>(MomentAccostEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_ACCOST_BATCH)
                .addQueryData("toUids", TextUtils.join(",", arrayOf(uid)))
                .setRequestHandler(object : RequestDefaultHandler<MomentAccostEvent, JsonData>() {

                    override fun processOriginData(originData: JsonData): MomentAccostEvent {
                        return MomentAccostEvent(requestTag, uid, fid)
                    }

                    override fun onRequestFinish(data: MomentAccostEvent) {
                        data.post()
                    }
                })
                .enqueue()
    }

    fun deleteMoment(requestTag: Any, uid: String, fid: String) {
        RequestCreator.create(DeleteMomentEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_MOMENT_DELETE_MOMENT)
                .addQueryData("fid", fid)
                .setRequestHandler(object : RequestDefaultHandler<DeleteMomentEvent, JsonData>() {
                    override fun processOriginData(originData: JsonData): DeleteMomentEvent {
                        return DeleteMomentEvent(requestTag, originData, uid, fid)
                    }

                    override fun onRequestFinish(data: DeleteMomentEvent) {
                        data.post()
                    }
                })
                .enqueue()
    }

    fun deleteComment(requestTag: Any, cid: String, fid: String) {
        RequestCreator.create(DeleteCommentEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_MOMENT_DELETE_COMMENT)
                .addPostData("commentId", cid)
                .setRequestHandler(object : RequestDefaultHandler<DeleteCommentEvent, JsonData>() {

                    override fun processOriginData(originData: JsonData): DeleteCommentEvent {
                        return DeleteCommentEvent(requestTag, originData, fid, cid)
                    }

                    override fun onRequestFinish(data: DeleteCommentEvent) {
                        data.post()
                    }
                })
                .enqueue()
    }
}